<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item>
          <div class="div1">仓库管理</div>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <div class="div2">出库类型</div>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 -->
    <!-- 添加客户等级 -->
    <div class="content">
      <p class="header">添加出库类型</p>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="类型名称">
          <el-input
            v-model="formInline.name"
            placeholder="请输入出库类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门选择">
          <div class="selectTree">
            <treeselect
              :options="options"
              v-model="formInline.dept"
              search-nested
              :normalizer="normalizer"
              noResultsText="未找到"
              placeholder="请选择"
            >
            </treeselect>
          </div>
        </el-form-item>

        <!-- <el-form-item label="是否需要发货">
          <el-select
            v-model="formInline.isSendGoods"
            placeholder="是否需要发货"
            clearable
            class="select"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否需要发货">
          <el-select
            v-model="formInline.isRefund"
            placeholder="是否需要退回"
            clearable
            class="select"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
            v-if="parArray.indexOf('addDelivery') != -1"
            >添加类型</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加客户等级 -->
    <!-- 管理客户等级 -->
    <div class="manage">
      <p class="header">管理出库类型</p>
      <div class="table">
        <el-table :data="tableData.list">
          <el-table-column
            type="index"
            label="序号"
            width="50px"
          ></el-table-column>

          <el-table-column scope="scope" label="类型名称" min-width="250">
            <template slot-scope="scope">
              <el-input
                v-if="scope.row.edit"
                v-model="scope.row.deliveryName"
              ></el-input>
              <span v-else>{{ scope.row.deliveryName }}</span>
            </template>
          </el-table-column>

          <el-table-column label="所属部门" min-width="250">
            <template slot-scope="scope">
              <treeselect
                v-show="scope.row.edit"
                :options="options"
                v-model="scope.row.orgCode"
                search-nested
                :normalizer="normalizer"
                noResultsText="未找到"
                placeholder="请选择"
              >
              </treeselect>
              <span v-show="!scope.row.edit">{{ scope.row.orgName }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="更新时间"
            prop="createdTime"
          ></el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="optain">
                <p
                  v-if="
                    !scope.row.edit && parArray.indexOf('deliveryPrevent') != -1
                  "
                  @click="scope.row.edit = true"
                >
                  编辑
                </p>

                <p
                  v-if="
                    scope.row.edit && parArray.indexOf('deliveryPrevent') != -1
                  "
                  @click="preservation(scope.row)"
                >
                  保存
                </p>
                <p
                  v-if="
                    scope.row.edit && parArray.indexOf('deliveryPrevent') != -1
                  "
                  @click="
                    scope.row.edit = false;
                    scope.row.deliveryName = scope.row.deliveryName1;
                  "
                >
                  取消
                </p>

                <p
                  @click="istopEvent(scope.row)"
                  v-if="
                    scope.row.isTop == '0' &&
                    parArray.indexOf('istopCode') != -1
                  "
                >
                  置顶
                </p>
                <p
                  @click="istopEvent(scope.row)"
                  v-if="
                    scope.row.isTop == '1' &&
                    parArray.indexOf('istopCode') != -1
                  "
                >
                  取消置顶
                </p>
                <p
                  v-if="parArray.indexOf('delectDevery') != -1"
                  @click="delect(scope.row, scope.$index)"
                >
                  删除
                </p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页start -->
        <div class="page" v-if="tableData.totalNum >= 7">
          <div class="block">
            <span class="demonstration">共{{ tableData.totalPage }}页</span>
            <span class="demonstration">总共{{ tableData.totalNum }}条</span>
            <el-pagination
              background=""
              @current-change="handleCurrentChange"
              :current-page.sync="currentPage1"
              :page-size="1"
              layout="prev, pager, next"
              prev-text="上一页"
              next-text="下一页"
              :total="tableData.totalPage"
            >
            </el-pagination>
          </div>
        </div>
        <!-- 分页end -->
      </div>
    </div>
    <!-- 管理客户等级 -->
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: {
    Treeselect,
  },
  data () {
    return {
      options: [],
      formInline: {
        name: "",
        dept: null,
        // isRefund: "",
        // isSendGoods: "",
      },
      tableData: [],
      currentPage1: 1,
      limits: "",
      parArray: [],
      normalizer (node) {
        return {
          id: node.orgCode,
          label: node.orgName,
          children: node.children,
        };
      },
    };
  },
  created () {
    this.brandlist("1");
    this.department();
    // 权限设定判断
    this.limits = JSON.parse(sessionStorage.getItem("limit"));
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === "deliveryTypeCode"
    );
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code);
    });
    // 权限设定判断   deliveryTypeCode
  },
  methods: {
    getMenuBtnList (menuTreeList, menuList, code) {
      for (let item of menuTreeList) {
        if (item.orgCode == code) {
          menuList.push({
            lable: item.orgName,
            orgCode: item.orgCode,
            type: item.type,
          });
        } else if (item.children && item.children.length > 0) {
          this.getMenuBtnList(item.children, menuList, code);
        }
      }
    },
    // 部门选择列表
    department () {
      this.axios
        .post(
          "/sys/v1/org/tree/select",
          {
            request: {
              orgName: "",
              orgType: "",
            },
            version: "",
          },
          {
            headers: {
              isLoading: false,
            },
          }
        )
        .then((res) => {
          if (res.code == 0) {
            this.options = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    /***列表查询***/
    brandlist (pageNo) {
      this.axios
        .post(
          "/order/v1/internal/delivery/config/list",
          {
            request: {
              deliveryName: "",
              pageNo: pageNo,
              orgCode: sessionStorage.getItem("orgCode"),
              pageSize: 10,
            },
            version: "",
          },
          {
            headers: {
              isloading: false,
            },
          }
        )
        .then((res) => {
          if (res.code == 0) {
            for (let i in res.data.list) {
              res.data.list[i].edit = false;
              res.data.list[i].deliveryName1 = res.data.list[i].deliveryName;
              if (!res.data.list[i].orgCode) {
                res.data.list[i].orgCode = null;
              }
            }
            this.tableData = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    istopEvent (row) {
      this.axios
        .get("/order/v1/internal/delivery/config/top?id=" + row.id, {
          headers: {
            isloading: false,
          },
        })
        .then((res) => {
          if (res.code == 0) {
            this.$message.success("置顶成功");
            this.brandlist(1);
          } else {
            this.$message.success(res.message);
          }
        });
    },
    /***列表查询***/
    handleCurrentChange (val) {
      this.brandlist(`${val}`);
    },
    /**添加出库类型**/
    onSubmit () {
      if (this.formInline.name == "") {
        this.$message.warning("出库名字不能为空");
      } else if (this.formInline.dept == "") {
        this.$message.warning("所属部门不能为空");
      } else {
        let newArray = [];
        this.getMenuBtnList(this.options, newArray, this.formInline.dept);
        this.axios
          .post(
            "/order/v1/internal/delivery/config/add",
            {
              request: {
                deliveryName: this.formInline.name,
                isRefund: this.formInline.isRefund,
                isSendGoods: this.formInline.isSendGoods,
                isTop: 0,
                orgCode: newArray.length ? newArray[0].orgCode : "",
                orgName: newArray.length ? newArray[0].lable : "",
              },
              version: "",
            },
            {
              headers: {
                isloading: false,
              },
            }
          )
          .then((res) => {
            // console.log(res);
            if (res.code == 0) {
              this.$message.success("添加成功");
              this.formInline.name = "";
              this.formInline.isRefund == "";
              this.formInline.isSendGoods == "";
              this.brandlist(1);
            } else {
              this.$message.error(res.message);
            }
          });
      }
    },
    // 添加出库类型
    /***删除数据对接***/
    delect (row, index) {
      this.$confirm("确定要删除该类型吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .get("/order/v1/internal/delivery/config/delete?id=" + row.id)
            .then((res) => {
              // console.log(res);
              if (res.code == 0) {
                this.tableData.list.splice(index, 1);
                this.brandlist("1");
                this.$message.success("删除成功");
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /***删除数据对接***/
    /***修改数据对接***/
    preservation (row) {
      if (!row.deliveryName || !row.orgCode) {
        this.$message.warning("不能为空");
        return;
      }
      let newArray = [];
      this.getMenuBtnList(this.options, newArray, row.orgCode);
      this.axios
        .post(
          "/order/v1/internal/delivery/config/update",
          {
            request: {
              id: row.id,
              deliveryName: row.deliveryName,
              isRefund: row.isRefund,
              isSendGoods: row.isSendGoods,
              isTop: 0,
              orgCode: newArray.length ? newArray[0].orgCode : "",
              orgName: newArray.length ? newArray[0].lable : "",
            },
          },
          {
            headers: {
              isloading: false,
            },
          }
        )
        .then((res) => {
          // console.log(res);
          this.brandlist("1");
          if (res.code == 0) {
            this.$message.success("修改成功");
          } else {
            this.$message.error(res.message);
          }
        });
    },
    /***修改数据对接***/
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}

.div2 {
  margin-left: -16px;
  background: url(../../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}

/deep/.el-input__icon {
  height: 30px;
  line-height: 30px;
}

.select {
  width: 150px;

  /deep/.el-input {
    width: 150px;
  }

  /deep/.el-input__inner {
    width: 150px;
  }
}

.content {
  padding: 10px;
  background: #fff;

  .el-button {
    padding: 6px 10px;
  }
}

/deep/.el-input {
  width: 200px;
}

.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.demo-form-inline {
  margin-top: 10px;
}

/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}

.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
}

/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}

/deep/.cell {
  text-align: center;
}

// /deep/.el-table__row .cell {}

.optain {
  width: 100%;
  text-align: center;
}

.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}

.optain {
  p {
    display: inline-block;
    margin-left: 20px;

    img {
      width: 20px;
      height: 20px;
    }
  }
}

.page {
  margin: 20px 0;
  text-align: right;
  width: 100%;

  span {
    font-size: 15px;
    color: #888;
    display: inline-block;
    margin-left: 10px;
    margin-top: -2px;
    vertical-align: middle;
  }

  .el-pagination {
    display: inline-block;
    vertical-align: middle;
  }
}
.selectTree {
  width: 250px;
  display: inline-block;
  vertical-align: middle;
  margin-right: 10px;
}
/deep/.vue-treeselect {
  height: 28px;
}
/deep/.vue-treeselect--searchable .vue-treeselect__input-container {
  height: 28px;
  line-height: 28px;
  width: 100%;
}
/deep/.vue-treeselect__single-value {
  font-size: 14px;
  line-height: 28px;
}
/deep/.vue-treeselect__menu-container {
  font-size: 15px;
}
/deep/.vue-treeselect__multi-value-item {
  height: 20px;
  line-height: 20px;
  padding: 0;
}
/deep/.vue-treeselect__input {
  height: 26px;
  line-height: 26px;
  outline: none;
  border: none;
  padding: 0;
}
/deep/.vue-treeselect__placeholder {
  font-size: 10px;
  line-height: 28px;
}
/deep/.vue-treeselect--has-value .vue-treeselect__multi-value {
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/deep/.vue-treeselect__control {
  height: 28px;
}
/deep/.el-table__body-wrapper {
  overflow: unset;
}
/deep/.cell {
  overflow: unset;
}
/deep/.el-table {
  overflow: unset;
}
</style>
